package Utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import vo.Point;

public class FileUtils {
	public static Hashtable<String, Double> readHashtableD(String fileName) {
		Hashtable<String, Double> userId_content = new Hashtable<String, Double>();
		String fileContent = FileUtils.readFile(fileName, "UTF-8");
		String[] lines = fileContent.split(Constants.LINE_SEPERATOR);
		for (int i = 0; i < lines.length; i++) {
			String items[] = lines[i].split(Constants.HASH_SEPERATOR);
			userId_content.put(items[0], Double.parseDouble(items[1]));
		}
		return userId_content;
	}

	public static Hashtable<String, String> readHashtableS(String fileName) {
		Hashtable<String, String> params = new Hashtable<String, String>();
		String fileContent = FileUtils.readFile(fileName, "UTF-8");
		String[] lines = fileContent.split("\n");
		for (int i = 0; i < lines.length; i++) {
			String items[] = lines[i].split(Constants.HASH_SEPERATOR);
			params.put(items[0], items[1]);
		}
		return params;
	}

	public static String readFile(String fileName, String encoding) {
		String result = null;
		try {
			File contentfile = new File(fileName);
			InputStream is = new FileInputStream(contentfile);
			InputStreamReader isr = new InputStreamReader(is, encoding);
			StringBuffer sb = new StringBuffer();
			BufferedReader in = new BufferedReader(isr);
			String inputLine;
			while ((inputLine = in.readLine()) != null) {
				sb.append(inputLine);
				sb.append("\n");
			}
			result = sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// public static String readHashtable(String fileName, String encoding) {
	// String result = null;
	// try {
	// File contentfile = new File(fileName);
	// InputStream is = new FileInputStream(contentfile);
	// InputStreamReader isr = new InputStreamReader(is, encoding);
	// StringBuffer sb = new StringBuffer();
	// BufferedReader in = new BufferedReader(isr);
	// String inputLine;
	// while ((inputLine = in.readLine()) != null) {
	// sb.append(inputLine);
	// sb.append("\n");
	// }
	// result = sb.toString();
	//
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return result;
	// }
	//
	public static void write(String path, String content) {
		try {
			File f = new File(path);
			if (f.exists()) {
				System.out.println("file existe");
			} else {
				System.out.println("creating ....");
				if (f.createNewFile()) {
					System.out.println("successful created");
				} else {
					System.out.println("fail to created");
				}
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(content);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("写文件完毕");
	}

	public static void writePoints(String path, ArrayList<Point> points) {
		try {
			File f = new File(path);
			if (f.exists()) {
				System.out.println("file existe");
			} else {
				System.out.println("creating ....");
				if (f.createNewFile()) {
					System.out.println("successful created");
				} else {
					System.out.println("fail to created");
				}
			}
			// 封装数据
			String content = "";
			for (Point point : points) {
				content += point.getX() + "," + point.getY() + "\n";
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(content);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("写文件完毕");
	}

	public static void copyFile(File src, File dest) throws Exception {
		try {
			FileChannel srcChannel = new FileInputStream(src).getChannel();
			FileChannel dstChannel = new FileOutputStream(dest).getChannel();
			dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
			srcChannel.close();
			dstChannel.close();
		} catch (IOException e) {
		}

	}

	public static void writeFileHashtable(Hashtable ht, String fileName, int order) {
		String content = "";
		Map.Entry[] entries = SortUtils.sortedHashtableByValue(ht, order);
		for (int i = 0; i < entries.length; i++)
			content += entries[i].getKey() + "#" + entries[i].getValue() + "\n";
		try {
			File f = new File(fileName);
			if (f.exists()) {
				System.out.println("file existe");
			} else {
				System.out.println("creating ....");
				if (f.createNewFile()) {
					System.out.println("successful created");
				} else {
					System.out.println("fail to created");
				}
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			output.write(content);
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("finished!");
	}

	public static void main(String args[]) {

	}
}
